package fhb.mobile.todoapp;

import java.util.Date;

import fhb.mobile.manager.Storage;
import fhb.mobile.todoapp.datamodell.ToDoItem;
import fhb.mobile.todoapp.datamodell.ToDoItemList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

public class EditToDoItemActivity extends Activity {
	
	private ImageButton featured = null;
	private ImageButton contacts = null;
	private ImageButton deadline = null;
	private Button saveButton = null;
	private Button deleteButton = null;
	
	private ToDoItem modifiedToDoItem = null;
	
	@Override
	public void onBackPressed() {
		Storage.removeToDo(modifiedToDoItem);
		startActivity(new Intent(EditToDoItemActivity.this, ToDoActivity.class));
		
		super.onBackPressed();
	}
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.todoliste_edit_item);
        
        featured = (ImageButton) findViewById(R.id.todoliste_edit_item_imageButton_featured);
        contacts = (ImageButton) findViewById(R.id.todoliste_edit_item_imageButton_add_Contacts);
        deadline = (ImageButton) findViewById(R.id.todoliste_edit_item_imageButton_change_deadline);
        saveButton = (Button) findViewById(R.id.todoliste_edit_item_button_save);
        deleteButton = (Button) findViewById(R.id.todoliste_edit_item_button_delete);
        
        modifiedToDoItem = ToDoItemList.getModifiedToDoItem();
        /*
        if (extras.getLong("modifiedItem") != -1)
        	modifiedToDoItem = toDoItem.clone();
        */	
        if (modifiedToDoItem.isFeatured())
			featured.setImageResource(android.R.drawable.btn_star_big_on);
		else
			featured.setImageResource(android.R.drawable.btn_star_big_off);
        	
        ((EditText) findViewById(R.id.todoliste_edit_item_editText_title)).setText(modifiedToDoItem.getTitle());
        ((EditText) findViewById(R.id.todoliste_edit_item_editText_content)).setText(modifiedToDoItem.getContent());
        // Java-Bug umgehen
        //deadline_date.setYear(deadline_date.getYear() - 1900);
        
        //Storage.getToDoItemList().setModifiedToDoItem(modifiedToDoItem);
        

        ((TextView) findViewById(R.id.todoliste_edit_item_textView_current_deadline)).setText(new Date(modifiedToDoItem.getDeadline()).toLocaleString());
        ((TextView) findViewById(R.id.todoliste_edit_item_textView_contacts_number)).setText(String.valueOf(modifiedToDoItem.getAssociatedContacts().size()));
        
        featured.setOnClickListener(
        		new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						modifiedToDoItem.setFeatured(!modifiedToDoItem.isFeatured());
						
						if (modifiedToDoItem.isFeatured())
							((ImageButton) v).setImageResource(android.R.drawable.btn_star_big_on);
						else
							((ImageButton) v).setImageResource(android.R.drawable.btn_star_big_off);
					}
				});
        
        contacts.setOnClickListener(
        		new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						modifiedToDoItem.setTitle(((EditText) findViewById(R.id.todoliste_edit_item_editText_title)).getText().toString());
						modifiedToDoItem.setContent(((EditText) findViewById(R.id.todoliste_edit_item_editText_content)).getText().toString());
						if (modifiedToDoItem.getTitle().length()==0){
							modifiedToDoItem.setTitle("Title");
						}
						Storage.updateToDo(modifiedToDoItem);
						
						Intent intent = new Intent(EditToDoItemActivity.this,ChangeAssociatedContactsActivity.class);
						startActivity(intent);
						finish();
						
					}
				});
        
        deadline.setOnClickListener(
        		new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						modifiedToDoItem.setTitle(((EditText) findViewById(R.id.todoliste_edit_item_editText_title)).getText().toString());
						modifiedToDoItem.setContent(((EditText) findViewById(R.id.todoliste_edit_item_editText_content)).getText().toString());
						if (modifiedToDoItem.getTitle().length()==0){
							modifiedToDoItem.setTitle("Title");
						}
						Storage.updateToDo(modifiedToDoItem);
						
						Intent intent = new Intent(EditToDoItemActivity.this, ChooseDeadlineActivity.class);
						startActivity(intent);
						finish();
					}
				});
        
        saveButton.setOnClickListener(
        		new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						modifiedToDoItem.setTitle(((EditText) findViewById(R.id.todoliste_edit_item_editText_title)).getText().toString());
						modifiedToDoItem.setContent(((EditText) findViewById(R.id.todoliste_edit_item_editText_content)).getText().toString());
						if (modifiedToDoItem.getTitle().length()==0){
							modifiedToDoItem.setTitle("Title");
						}
						Storage.updateToDo(modifiedToDoItem);
						startActivity(new Intent(EditToDoItemActivity.this, ToDoActivity.class));
						EditToDoItemActivity.this.finish();
					}
				});
        
        deleteButton.setOnClickListener(
        		new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						Storage.removeToDo(modifiedToDoItem);
						startActivity(new Intent(EditToDoItemActivity.this, ToDoActivity.class));
						EditToDoItemActivity.this.finish();
					}
				});
    }
}
